package it.unicam.fishpass.prism.parser;

/**
 * Represents an assignment expression contained at the end of an automata arc.
 * The object can be used to calculate assignments for clocks variables.
 * 
 * @author Andrea Baiocco
 */
public class Assign {

	private String var;

	private ArithmeticExpression aExpr;
	private BooleanExpression bExpr;

	/**
	 * assignment of the string var and of the arithmetic expression
	 * 
	 * @param expr
	 * @param var
	 */
	public Assign(String var, ArithmeticExpression expr) {
		this.var = var;
		this.aExpr = expr;
		bExpr = null;
	}

	/**
	 * @param var
	 * @param expr
	 */
	public Assign(String var, BooleanExpression expr) {
		this.var = var;
		this.bExpr = expr;
		aExpr = null;
	}

	String getVar() {
		return var;
	}
	
	@Override
	public String toString() {
		if (aExpr != null)
			return var + " " + aExpr.toString();
		return var + " " + bExpr.toString();
	}

	/**
	 * Calculates the assignment for the contained expression. The boolean
	 * expression is represented as numeric 1 (true) and 0 (false).
	 */
	int doAssignment(Declaration[] decs, int[] clocks, float[] vars) {
		if (aExpr != null)
			return aExpr.calculateExpression();	// TODO rivedere Aexpr per variabile numerica "v" in grammatica
		return bExpr.evaluateExpression(decs, clocks, vars) ? 1 : 0;
	}
}